﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using Yz.Base;
using Yz.Core.Extensions;
using Yz.DB.EFCore.Dal;
using Yz.Model.Constants;
using Yz.Model.Entities;
using Yz.Model.Views;

namespace Yz.Manage.Controllers
{
    [Authorize(Roles = UserRoleName.Company)]
    public class StudentController : BaseController
    {
        private readonly IDbAccess<Student> _studentDbAccess;
        private readonly IDbAccess<SupplementSetting> _supplementSettingDbAccess;
        public StudentController(IDbAccess<Student> studentDbAccess, IDbAccess<SupplementSetting> supplementSettingDbAccess)
        {
            _studentDbAccess = studentDbAccess;
            _supplementSettingDbAccess = supplementSettingDbAccess;
        }
        /// <summary>
        /// 补录申请
        /// </summary>
        /// <returns></returns>
        public IActionResult Apply()
        {
            return View();
        }
        /// <summary>
        /// 学籍列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获得补录设置
        /// </summary>
        /// <returns></returns> 
        public async Task<JsonResult> GetSupplementSet()
        {
            int companyId = base.CompanyId;
            var sptSet = await _supplementSettingDbAccess.GetModelAsync<VmSupplementSetting>(p => p.CompanyId == companyId);
            return Json(sptSet.ToVmData());
        }
        /// <summary>
        /// 添加补录信息
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Save(Student student)
        {
            int companyId = base.CompanyId;
            if (student.Name.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入姓名" });
            }
            if (student.IDNo.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入证件号码" });
            }
            if (student.MobilePhone.IsEmpty() || !student.MobilePhone.IsMobilePhone())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入正确的手机号" });
            }
            if (student.Area.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入地区" });
            }
            if (student.Domicile.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入户口" });
            }
            if (student.SchoolName.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入学校" });
            }
            if (student.SpecialityName.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "请输入专业" });
            }
            var dtNow = DateTime.Now;
            student.UserId = base.UserId;
            student.UserName = base.UserName;
            student.CreateTime = dtNow;
            student.UpdateTime = dtNow;
            student.CompanyId = companyId;
            var retCode = await _studentDbAccess.AddAsync(student);
            return Json(retCode.ToVmReturn());
        }
        /// <summary>
        /// 分页查询学籍信息
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="name"></param>
        /// <param name="mobilePhone"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetList(int pageSize, int pageIndex, string name, string mobilePhone, string userName)
        {
            var page = new ParaPage { SkipNum = (pageIndex - 1) * pageSize, TakeNum = pageSize };
            var orders = new List<ParaOrder<Student>>() {
                new ParaOrder<Student> { Order = p =>   p.UpdateTime , IsAsc = false }
            };
            int companyId = base.CompanyId;
            Expression<Func<Student, bool>> where = p => p.CompanyId == companyId;
            int userId = base.UserId;
            if (User.IsInRole(UserRoleName.CompanyAdmin))
            {
                if (!userName.IsEmpty())
                {
                    where = where.And(c => c.UserName == userName);
                }
            }
            else
            {
                where = where.And(c => c.UserId == userId);
            }
            if (!name.IsEmpty())
            {
                where = where.And(c => c.Name == name);
            }
            if (!mobilePhone.IsEmpty())
            {
                where = where.And(c => c.MobilePhone == mobilePhone);
            }

            var list = await _studentDbAccess.GetListByPageAsync<VmStudent>(where, page, orders);
            return Json(list.ToVmPageList());
        }
    }
}